﻿using DevExpress.Web.ASPxFileManager;
using DevExpress.Web.ASPxUploadControl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utility1;

namespace eDream.admin
{
    public partial class Documnet : System.Web.UI.UserControl
    {
        public string pathUrl = "";
        string UploadDirectory = "~/TaiLieu/Msmart/";
        protected void Page_Load(object sender, EventArgs e)
        {
            pathUrl = WebUtils.pathUrl();
           
            fileManager.Settings.RootFolder =  "/TaiLieu/Msmart/";
            fileManager.Settings.ThumbnailFolder = "/TaiLieu/Thum/";
            if (!IsPostBack)
            {
                UserDB user_obj = (UserDB)Session[Global.SESS_USER];
                if (user_obj.q == 99)
                {
                    ApplyFileManagerSettings();
                    p1.Visible = true;
                    pnNote.Visible = true;
                }
                else if (user_obj.money == 590)
                {
                    ApplyFileManagerSettings_no();
                    pnNote.Visible = true;
                }
                else
                {
                    fileManager.Visible = false;
                    pnNote.Visible = false;
                }
                    
                
            }
        }

        protected void fileManager_FileUploading(object sender, FileManagerFileUploadEventArgs e)
        {
           // ValidateSiteEdit(e);
            //fileManager.FindControl();
        }

        protected void fileManager_ItemRenaming(object sender, FileManagerItemRenameEventArgs e)
        {
            ValidateSiteEdit(e);
        }

        protected void fileManager_ItemMoving(object sender, FileManagerItemMoveEventArgs e)
        {
            ValidateSiteEdit(e);
        }

        protected void fileManager_ItemDeleting(object sender, FileManagerItemDeleteEventArgs e)
        {
            ValidateSiteEdit(e);
        }

        protected void fileManager_FolderCreating(object sender, FileManagerFolderCreateEventArgs e)
        {
            ValidateSiteEdit(e);
        }

        void ValidateSiteEdit(FileManagerActionEventArgsBase e)
        {
            e.Cancel = eUtils.IsSiteMode;
            e.ErrorText = "Upload file";
        }

        void ApplyFileManagerSettings()
        {
            fileManager.SettingsEditing.AllowMove = true;
            fileManager.SettingsEditing.AllowDelete = true;
            fileManager.SettingsEditing.AllowRename = true;
            fileManager.SettingsEditing.AllowCreate = true;
            fileManager.SettingsToolbar.ShowPath = true;
            fileManager.SettingsToolbar.ShowRefreshButton = true;
            fileManager.SettingsToolbar.ShowFilterBox = true;
            fileManager.SettingsToolbar.ShowDownloadButton = true;
            fileManager.SettingsFolders.ShowLockedFolderIcons = true;
            fileManager.SettingsFolders.ShowFolderIcons = true;
            fileManager.SettingsFolders.EnableCallBacks = true;
            fileManager.SettingsFolders.Visible = true;
            fileManager.SettingsUpload.Enabled = true;

            fileManager.SettingsUpload.UseAdvancedUploadMode = true;
            fileManager.SettingsUpload.AdvancedModeSettings.EnableMultiSelect = true;
        }

        void ApplyFileManagerSettings_no()
        {
            fileManager.SettingsEditing.AllowMove = false;
            fileManager.SettingsEditing.AllowDelete = false;
            fileManager.SettingsEditing.AllowRename = false;
            fileManager.SettingsEditing.AllowCreate = false;
            fileManager.SettingsToolbar.ShowPath = true;
            fileManager.SettingsToolbar.ShowRefreshButton = true;
            fileManager.SettingsToolbar.ShowFilterBox = true;
            fileManager.SettingsToolbar.ShowDownloadButton = true;
            fileManager.SettingsFolders.ShowLockedFolderIcons = false;
            fileManager.SettingsFolders.ShowFolderIcons = true;
            fileManager.SettingsFolders.EnableCallBacks = true;
            fileManager.SettingsFolders.Visible = true;
            fileManager.SettingsUpload.Enabled = false;
          
            fileManager.SettingsUpload.UseAdvancedUploadMode = false;
            fileManager.SettingsUpload.AdvancedModeSettings.EnableMultiSelect = false;
        }

        protected void btnUploadFile_Click(object sender, EventArgs e)
        {
            /*
            try
            {
                
                string sFolderPath = Server.MapPath(@"TaiLieu\" + txtPath.Text.Trim());
                HttpPostedFile myFile = FileUpload1.PostedFile;
                string sFileName = myFile.FileName;
              
                myFile.SaveAs(
                    string.Format(@"{0}\{1}", sFolderPath, sFileName));
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
             * */
        }

        protected void UploadControl_FileUploadComplete(object sender, FileUploadCompleteEventArgs e)
        {
            if (Session["UploadDirectory"] == null)
                Session["UploadDirectory"] = UploadDirectory;
            string resultFilePath = Session["UploadDirectory"].ToString() + e.UploadedFile.FileName;
            e.UploadedFile.SaveAs(MapPath(resultFilePath));

            //UploadingUtils.RemoveFileWithDelay(e.UploadedFile.FileName, MapPath(resultFilePath), 5);

            string file = string.Format("{0} ({1}KB)", e.UploadedFile.FileName, e.UploadedFile.ContentLength / 1024);
            string url = ResolveClientUrl(resultFilePath);
            e.CallbackData = file + "|" + url;
            
        }

        protected void txtPath_ValueChanged(object sender, EventArgs e)
        {
           Session["UploadDirectory"] = "~/TaiLieu/" + txtPath.Text + "/";
        }
    }
}